package net.zhengxinyun.performance.App.service.UnionOtherSys;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.App.entity.EvaluateScoreEntity;
import net.zhengxinyun.performance.Util.SystemApiUtl;
import net.zhengxinyun.performance.Util.httpsUitl.HttpClientService;
import net.zhengxinyun.performance.entity.SysUnionCode;
import net.zhengxinyun.performance.mapper.sysUnion.SysUnionCodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Date: 2020/3/18 16:56
 */
@Service
public class UnionOtherSysServiceImpl implements UnionOtherSysService {


    @Autowired
    private SysUnionCodeMapper codeMapper;


    @Override
    public List<SysUnionCode> selectListCode() {
        return codeMapper.selectListCode();
    }

    /**
     * 获取督查系统获取用户Id
     * @param leaderPhone
     * @return
     * @throws IOException
     */
    @Override
    public HashMap<String, Object> DC_userInfo(String leaderPhone) throws IOException {

        HashMap<String ,Object> duChaData = new HashMap<>();
        String nameValuePairs ="{\"mobile\":\"" +leaderPhone.trim()+"\"}";
        String param2 = JSONObject.toJSON(nameValuePairs).toString();
        JSONObject jsonObject = HttpClientService.sendPost(SystemApiUtl.SERVICEADDRESS + ":" + SystemApiUtl.DUCAH_TEST+SystemApiUtl.DC_GET_LEADSCORE, param2, "http");
        if(ObjectUtils.isNotBlank(jsonObject))
        {
            JSONArray duchalead = jsonObject.getJSONArray("data");
            if (ObjectUtils.isNotBlank(duchalead)){
                //System.out.println(duchalead);
                JSONObject dataobj = (JSONObject) duchalead.get(0);
                if (ObjectUtils.isNotBlank(dataobj)){
                    JSONObject leadobj = dataobj.getJSONObject("leader");
                    if(ObjectUtils.isNotBlank(leadobj))
                    {
                        String userId = leadobj.getString("userId");
                        String role = leadobj.getString("role");
                        duChaData.put("userId",userId);
                        duChaData.put("role",role);
                    }
                }
            }
        }

        return duChaData;
    }

    /**
     * 关联督查批示件
     * @return
     */
    @Override
    public List<EvaluateScoreEntity> DC_PishiJian(String userId, String criteriaType) throws IOException {
        String nameValuePairs ="{\"userId\":\"" +userId.trim()+"\",\"demo\":\"2\"}";
        String param2 = JSONObject.toJSON(nameValuePairs).toString();
        JSONArray duChaObj = getDuChaObj(param2);
        List<EvaluateScoreEntity> scoreLists =null;
        if(ObjectUtils.isNotBlank(duChaObj)){
            scoreLists = setEvaScoreValues(duChaObj, criteriaType,"DCPSJ");
        }
        return scoreLists;
    }
    private JSONArray getDuChaObj(String param) throws IOException {
        JSONArray zhongdianWorkobj = null;
        JSONObject jsonObject = HttpClientService.sendPost("http://120.78.158.120:8077/app/leader/queryTask", param, "http");
        List<EvaluateScoreEntity> scoreLists = null;
        if(ObjectUtils.isNotBlank(jsonObject)){
            JSONObject objWork = jsonObject.getJSONObject("data");
            if(ObjectUtils.isNotBlank(objWork))
            {

                zhongdianWorkobj = objWork.getJSONArray("list");
            }
        }

        return  zhongdianWorkobj;
    }

     private List<EvaluateScoreEntity> setEvaScoreValues(JSONArray array,String criteriaType,String connects){
        List<EvaluateScoreEntity>  scoreLists = new ArrayList<>();
        for (int i = 0; i < array.size(); i++) {
                JSONObject obj = (JSONObject) array.get(i);
                EvaluateScoreEntity entity = new EvaluateScoreEntity();
                entity.setAssessment(obj.getString("mainName"));
                entity.setItemid(obj.getString("sysId"));
                entity.setSysCode(connects);
                entity.setCriteriaType(criteriaType);
                entity.setCalculates(0);
                scoreLists.add(entity);
        }
        return scoreLists;
    }

    /**
     * 督查系统重点工作
     * @param userId
     * @param criteriaType
     * @return
     * @throws IOException
     */
    @Override
    public List<EvaluateScoreEntity> DC_ZhongDianWork(String userId, String criteriaType) throws IOException {
        String nameValuePairs ="{\"userId\":\"" +userId.trim()+"\",\"demo\":\"1\"}";
        String param2 = JSONObject.toJSON(nameValuePairs).toString();
        List<EvaluateScoreEntity> scoreLists =null;
        //从督察系统获取数据
        JSONArray zhongdianWorkobj = getDuChaObj(param2);
        if(ObjectUtils.isNotBlank(zhongdianWorkobj)){
             scoreLists = setEvaScoreValues(zhongdianWorkobj,criteriaType,"DCZDWORK");
            }

    return scoreLists;
    }

    /**
     * 重大在建项目 五个一批
     * @param leaderPhone
     * @param ctriteriaType
     * @return
     */
    @Override
    public List<EvaluateScoreEntity> ZD_ZaiJianProject(String leaderPhone, String ctriteriaType) throws IOException {
        String nameValuePairs ="{\"userPhone\":\"" +leaderPhone.trim()+"\",\"status\":\"4\"}";
        String param2 = JSONObject.toJSON(nameValuePairs).toString();
        JSONObject jsonObject = HttpClientService.sendPost("http://120.78.158.120:8099/zhApp/leader/queryProjectDetail", param2, "http");
        List<EvaluateScoreEntity> scoreLists = null;
        if(ObjectUtils.isNotBlank(jsonObject)){
            JSONArray yipiListsObj = jsonObject.getJSONArray("data");
                if(ObjectUtils.isNotBlank(yipiListsObj))
                {
                    scoreLists = new ArrayList<>();

                    for (int i = 0; i < yipiListsObj.size(); i++) {
                        JSONObject obj = (JSONObject) yipiListsObj.get(i);
                        EvaluateScoreEntity entity = new EvaluateScoreEntity();
                        entity.setAssessment(obj.getString("name"));
                        entity.setItemid(obj.getString("sysId"));
                        entity.setSysCode("ZD");
                        entity.setCriteriaType(ctriteriaType);
                        entity.setCalculates(0);
                        scoreLists.add(entity);
                    }
                }
        }
        return scoreLists;
    }
}
